/*
Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package Translate;

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
import GuiTools.*;
import Mainframe.ExtendButton;
import Settings.Language;
import Storage.Meaning;

/**
 * @author Christoph Schnack
 *
 */
public class MeaningPanel extends JPanel {
	ExtendButton extendBtn;
	JPanel panel,infoPanel;
	GridBagConstraints gbc = new GridBagConstraints();
	TribuitFrame parentFrame;
	
	public MeaningPanel(TribuitFrame parentFrameArg, Meaning m,int num,Language LANG,Vector<String> metaKeys){
		super();
		panel = this;
		parentFrame = parentFrameArg;
		
		gbc.fill = GridBagConstraints.NONE;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		//gbc.weighty = 1;
		//gbc.weightx = 1;
		
		JPanel chiefMeaningPanel = new JPanel(new GridBagLayout());
		JLabel categoryLabel = new JLabel(m.hasCategory()?" ["+m.getCategory()+"]":"");
		categoryLabel.setFont(new Font(Font.SERIF,Font.ITALIC,18));
		categoryLabel.setOpaque(false);
		JLabel meaningLabel = new JLabel(m.getMeaningWithArticle());
		meaningLabel.setFont(new Font(Font.SERIF,Font.BOLD,18));
		meaningLabel.setOpaque(false);
		chiefMeaningPanel.add(meaningLabel,gbc);
		chiefMeaningPanel.add(categoryLabel,gbc);
		JPanel filler = new JPanel();
		filler.setOpaque(false);
		gbc.weightx = 1;
		chiefMeaningPanel.add(filler,gbc);
		chiefMeaningPanel.setOpaque(false);
		
		extendBtn = new ExtendButton(">>",Color.black,Color.red);
		extendBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(extendBtn.getButtonText().equals("<<")){
					panel.remove(infoPanel);
					extendBtn.setButtonText(">>");
				}
				else{
					panel.add(infoPanel,gbc);
					extendBtn.setButtonText("<<");
				}
				parentFrame.packAndKeepBounds();
			}
		});
		
		JPanel extendBtnPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		extendBtnPanel.add(extendBtn);
		//extendBtnPanel.setBackground(Color.gray);
		
		JPanel metaPanel = new JPanel();
		metaPanel.setLayout(new GridBagLayout());
		metaPanel.setOpaque(false);
		if(m.hasMeta()){
			GridBagConstraints metgbc = new GridBagConstraints();
			metgbc.anchor = GridBagConstraints.NORTHWEST;
			metgbc.weightx = 0;
			int gbcY = 2;
			String metA;
			for(String key : metaKeys){
				metA = m.getMetaElement(key);
				if(metA!=null && !metA.equals("")){
					metgbc.gridy = gbcY++;
					metgbc.gridx = 0;
					metgbc.insets = new Insets(5,15,0,0);
					JLabel mNLabel = new JLabel(key+":");
					mNLabel.setOpaque(false);
					mNLabel.setFont(new Font(Font.SERIF,Font.PLAIN,mNLabel.getFont().getSize()));
					metaPanel.add(mNLabel,metgbc);
					metgbc.gridx = 1;
					metgbc.insets = new Insets(5,5,0,0);
					metaPanel.add(new JLabel(metA),metgbc);
				}
			}
			metgbc.gridx = 1;
			metgbc.weightx = 1;
			metgbc.gridheight = 100;
			JPanel fill = new JPanel();
			fill.setOpaque(false);
			metaPanel.add(fill,metgbc);
		}
		CustomTextArea 
			synonymsTA = new CustomTextArea(m.hasSynonyms()?"( "+m.getSynonymsAsString()+" )":"n.a."),
			ecTA = new CustomTextArea((m.hasExampleSentence())?m.getExampleSentence():"n.a."),
			noteTA = new CustomTextArea(m.hasNote()?m.getNote():"n.a.");
		
		JPanel synonymsTAPanel = new JPanel();
		synonymsTAPanel.add(synonymsTA);
		synonymsTAPanel.setOpaque(false);
		
		JLabel synonymsLabel = new JLabel("<HTML><BODY>"+(m.hasSynonyms()?"( "+m.getSynonymsAsString()+" )":"n.a.")+"</body></html>");
		
		JLabel phoneticLabel = new JLabel(((m.hasPhonetic())?"["+m.getPhonetic()+"]":"n.a."));
		phoneticLabel.setFont(new Font(Font.SERIF,Font.PLAIN,15));
		
		JPanel meaningPanel = new JPanel();
		meaningPanel.setLayout(new GridBagLayout());
		//meaningPanel.setBackground(Color.white);
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.weighty = 1;
		gbc.weightx = 1;
		gbc.gridy++;
		meaningPanel.add(chiefMeaningPanel,gbc);
		JPanel mFiller = new JPanel();
		mFiller.setOpaque(false);
		gbc.gridy++;
		if(m.hasSynonyms()){
			meaningPanel.add(synonymsLabel,gbc);
		}
		else meaningPanel.add(mFiller,gbc);
		if(m.hasMeta()){
			gbc.gridy++;
			meaningPanel.add(new TitleLabel(LANG.getMetainformation()+":",12,Color.gray),gbc);
			gbc.gridy++;
			meaningPanel.add(metaPanel,gbc);
		}
		gbc.gridy++;
		meaningPanel.add(extendBtnPanel,gbc);
		//meaningPanel.setBorder(BorderFactory.createLineBorder(Color.red));
		
		infoPanel = new JPanel();
		infoPanel.setLayout(new GridBagLayout());
		infoPanel.setBackground(Color.lightGray);
		
		gbc.weightx = 0;
		gbc.weighty = 0;
		gbc.gridx = GridBagConstraints.REMAINDER;
		gbc.gridy = GridBagConstraints.RELATIVE;
		gbc.insets = new Insets(5,0,0,0);
		infoPanel.add(new TitleLabel(LANG.getPhonetic()+":"),gbc);
		infoPanel.add(phoneticLabel, gbc);
		infoPanel.add(new TitleLabel(LANG.getNote()+":"),gbc);
		infoPanel.add(noteTA,gbc);
		infoPanel.add(new TitleLabel(LANG.getExample()+":"),gbc);
		infoPanel.add(ecTA,gbc);
		gbc.gridx = 1;
		gbc.weightx = 1;
		gbc.gridheight = 6;
		JPanel fill = new JPanel();
		fill.setOpaque(false);
		infoPanel.add(fill,gbc);
		gbc.weightx = 0;
		gbc.gridheight = 1;

		gbc.gridy = 0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.weighty = 1;
		gbc.weightx = 1;
		gbc.gridx = 0;
		gbc.insets = new Insets(0,0,0,0);
		this.setLayout(new GridBagLayout());
		this.add(meaningPanel,gbc);
		this.setBackground(Color.gray);
		//this.setBorder(BorderFactory.createLineBorder(Color.black));
		gbc.gridy = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 0;
		gbc.insets = new Insets(10,10,10,10);
	}
	
	private class TitleLabel extends JLabel{
		public TitleLabel(String txt, int fontSize, Color color){
			super(txt!=null?txt:"");
			this.setFont(new Font(Font.SERIF,Font.BOLD,(fontSize>0)?fontSize:14));
			this.setForeground(color!=null?color:Color.black);
		}
		public TitleLabel(String txt){
			super(txt!=null?txt:"");
			this.setFont(new Font(Font.SERIF,Font.BOLD,14));
		}
	}
}
